package org.example.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.Exception.RollBackException;
import org.example.dao.CommentDao;
import org.example.dao.DiscussionDao;
import org.example.dao.UserDao;
import org.example.domain.Comment;
import org.example.domain.Discussion;
import org.example.domain.PageBean;
import org.example.domain.User;
import org.example.domain.response.ResponseResult;
import org.example.services.CommentService;
import org.example.utils.PageConvertPageBeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
@Transactional
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentDao commentDao;
    @Resource
    private DiscussionDao discussionDao;
    @Resource
    private UserDao userDao;

    @Override
    public ResponseResult saveComment(Comment comment) {
        try {
            Discussion discussion = discussionDao.selectById(comment.getDiscussionId());
            if(discussion == null){
                log.info("新增评论：讨论不存在");
                return ResponseResult.failed("讨论ID不存在");
            }
            User user = userDao.selectById(comment.getUserId());
            if(user == null){
                log.info("新增评论：用户不存在");
                return ResponseResult.failed("用户ID不存在");
            }
            commentDao.insert(comment);
            log.info("新增评论：成功{}",comment);
            return ResponseResult.success("新增评论成功",comment);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("新增评论：失败");
            throw new RollBackException("新增评论失败");
        }
    }

    @Override
    public ResponseResult deleteCommentById(int commentId) {
        try {
            Comment comment = commentDao.selectById(commentId);
            if(comment == null){
                log.info("删除评论：评论不存在");
                return ResponseResult.failed("评论不存在");
            }
            commentDao.deleteById(commentId);
            log.info("删除评论：成功{}",comment);
            return ResponseResult.success("删除评论成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("删除评论：失败");
            throw new RollBackException("删除评论失败");
        }
    }

    @Override
    public ResponseResult getCommentByDiscussionId(int discussionId, int currentPage) {
        try {
            Discussion discussion = discussionDao.selectById(discussionId);
            if(discussion == null){
                log.info("通过讨论ID获取评论：讨论ID不存在");
                commentDao.deleteByDiscussionId(discussionId);
                return ResponseResult.failed("讨论ID不存在");
            }
            Page<Comment> page = new Page<>(currentPage,10);
            LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            commentLambdaQueryWrapper.orderByDesc(Comment::getCreateTime);
            commentLambdaQueryWrapper.eq(Comment::getDiscussionId,discussionId);
            Page<Comment> commentPage = commentDao.selectPage(page, commentLambdaQueryWrapper);
            PageBean<Comment> pageBean = PageConvertPageBeanUtil.convert(commentPage);
            log.info("通过讨论ID获取评论：成功{}",pageBean);
            return ResponseResult.success("获取评论成功",pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("通过讨论ID获取评论：失败");
            return ResponseResult.failed("获取评论失败");
        }
    }
}
